package day2;

import java.util.Arrays;
import java.util.Scanner;

//学生管理系统类
public class StudentManagement {
    //学生对象组
    private Student[] stus;
    //实际学生个数
    private  int counter;
    //键盘录入对象
    private Scanner in;

    //无参构造，默认容量为100
    public StudentManagement(){
        stus = new Student[100];
        in = new Scanner(System.in);
    }
    //有参构造，size表示初始容量
    public  StudentManagement(int size){
        stus = new Student[size];
        in = new Scanner(System.in);
    }
    //输出操作提示
    public void prompt() {
        System.out.println("************************");
        System.out.println("*Student Management*");
        System.out.println("************************");
        System.out.println("* 1)Add");
        System.out.println("* 2)Delete");
        System.out.println("* 3)Update");
        System.out.println("* 4)Select");
        System.out.println("* 0)Quit");
        System.out.println("************************");
        System.out.print("Please choose your operate:");
    }
    //根据用户录入的学生名称，找到学生在数组中的索引返回
    private int findStudent(){
        int index = -1;
        String name;
        System.out.println("Please input name:");
        name = in.next();
        for (int i = 0; i < counter; i++) {//不要用stus.length,会出现空指针异常
            if(name.equals(stus[i].getName())){
                index = i;
            }
        }
        return  index;
    }
    //增加学生
    public  void  addStudent(){
        String name;
        int age;
        String gender;
        Student s;
        //判断是否已满
        if (counter>=stus.length){
            System.out.println("Add failure,too many student!");
            //扩容
            stus = Arrays.copyOf(stus, stus.length * 2);
            return;
        }
        System.out.print("Please input name:");
         name = in.next();
        System.out.print("Please input age:");
         age = in.nextInt();
        System.out.print("Please input gender:");
         gender = in.next();
         s = new Student(name, age, gender);

        // 关键代码
        stus[counter] = s;
        counter++;
        System.out.println("Add student success!");
    }

    //删除学生
    public  void  deleteStudent(){
        int index =  findStudent();
        if(index==-1){
            System.out.println("no student");
            return;
        }
        for (int i = index; i < counter; i++) {
            stus[i]=stus[i+1];
        }
        stus[counter - 1] = null;
        counter--;
        System.out.println("Delete student success!");
    }

    //修改学生
    public  void updateStudent(){
        String name;
        int age;
        String gender;
        Student s;
        int index =  findStudent();
        if(index==-1){
            System.out.println("no student");
        }
        System.out.print("Please input new name:");
        name = in.next();
        System.out.print("Please input new age:");
        age = in.nextInt();
        System.out.print("Please input new gender:");
        gender = in.next();
//        s.setAge(age);
//        s.setName(name);
//        s.setGender(gender);
        s = new Student(name, age, gender);
        stus[index] = s;
        System.out.println("Update student success!");
    }
    //查询学生
    public  void  selectStudent(){
       int index =  findStudent();
       if(index==-1){
           System.out.println("UNselect");
       }
       Student s = stus[index];
       s.show();
    }

    public static void main(String[] args) {
        //实例化对象
        StudentManagement sm = new StudentManagement();
        //用户操作标志
        int option = -1;
        Loop:while(true){
            //打印提示信息
            sm.prompt();
            //获取用户操作
            option = sm.in.nextInt();
            if (option < 0 || option > 4) {
                continue;
            }

            switch (option) {
                case 1:
                    sm.addStudent();
                    break;
                case 2:
                    sm.deleteStudent();
                    break;
                case 3:
                    sm.updateStudent();
                    break;
                case 4:
                    sm.selectStudent();
                    break;
                case 0:
                    break Loop;
            }
        }
        System.out.println("游戏结束，byebye");
    }

    }


